package com.xoa.service.workflow.FlowAssign.impl;

import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.workflow.FlowAssignMapper;
import com.xoa.dao.workflow.FlowProcessMapper;
import com.xoa.dao.workflow.FlowRunPrcsMapper;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.fixAssets.EduFixAssets;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.*;
import com.xoa.service.work.impl.WorkServiceImpl;
import com.xoa.service.workflow.FlowAssign.FlowAssignService;
import com.xoa.service.workflow.flowtype.FlowProcessService;
import com.xoa.util.AjaxJson;
import com.xoa.util.CookiesUtil;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class FlowAssignServiceImpl implements FlowAssignService {
    @Autowired
    FlowAssignMapper flowAssignMapper;
    @Autowired
    FlowProcessMapper flowProcessMapper;
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    FlowRunPrcsMapper flowRunPrcsMapper;
    @Autowired
    WorkServiceImpl workService;

    @Override
    public ToJson<FlowProcess> selectFeedBack(Integer flowId, Integer prcsId) {
        ToJson<FlowProcess> toJson = new ToJson<FlowProcess>();
        try {
            FlowProcess process = flowProcessMapper.findProcess(flowId, prcsId);
            List<FlowProcess> list = new ArrayList<>();
            list.add(process);
            toJson.setObj(list);
            toJson.setFlag(1);
            toJson.setMsg("OK");
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    @Override
    public ToJson<FlowAssign> insertAssign(FlowAssignWithBLOBs flowAssign) {
        ToJson<FlowAssign> toJson = new ToJson<FlowAssign>();
        try {
            int i = flowAssignMapper.insert(flowAssign);
            toJson.setFlag(1);
            toJson.setMsg("OK");
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    @Override
    public ToJson<FlowAssign> query(FlowAssign flowAssign, HttpServletRequest request) {
        ToJson<FlowAssign> json = new ToJson<FlowAssign>(1, "error");
        try {
            List<FlowAssign> query = flowAssignMapper.query(flowAssign);
            for (FlowAssign flowAssign1 :
                    query) {
                if (flowAssign1.getAssignUser() != null && flowAssign1.getAssignUser() != "") {
                    flowAssign1.setAssignUserName(usersMapper.getUsernameByUserId(flowAssign1.getAssignUser()));
                }
                if (flowAssign1.getHandleUser() != null && flowAssign1.getHandleUser() != "") {
                    flowAssign1.setHandleUserName(usersMapper.getUsernameByUserId(flowAssign1.getHandleUser()));
                }
                String sqlType = "xoa" + (String) request.getSession().getAttribute(
                        "loginDateSouse");
                List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(flowAssign1.getAttachmentName(), flowAssign1.getAttachmentId(), sqlType, "flowassign");
                List<Attachment> fdAttachment = GetAttachmentListUtil.returnAttachment(flowAssign1.getFdAttachmentName(), flowAssign1.getFdAttachmentId(), sqlType, "flowassign");
                flowAssign1.setAttachment(attachments);
                flowAssign1.setFdAttachment(fdAttachment);
            }
            if (query.size() > 0) {
                json.setMsg("ok");
                json.setObject(query);
                json.setFlag(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<FlowAssignWithBLOBs> updateFlowAssign(String assignId,
                                                        String fdAttachmentID,
                                                        String fdAttachmentName,
                                                        String taskFeedback,
                                                        Integer id,
                                                        String smsType,
                                                        String smsContent,
                                                        HttpServletRequest request,
                                                        Integer runId,
                                                        Integer flowId,
                                                        Integer prcsId,
                                                        Integer currentPrcsId,
                                                        String runName,
                                                        String tableName,
                                                        String tabId) {
        ToJson<FlowAssignWithBLOBs> toJson = new ToJson<FlowAssignWithBLOBs>();
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
            FlowAssignWithBLOBs flowAssign = new FlowAssignWithBLOBs();
            flowAssign.setAssignId(Integer.parseInt(assignId));
            flowAssign.setFdAttachmentId(fdAttachmentID);
            flowAssign.setFdAttachmentName(fdAttachmentName);
            flowAssign.setTaskFeedback(taskFeedback);
            flowAssign.setReceiveTime(new Date());
            flowAssign.setHandleStatus(2);
         /*   FlowRunPrcs flowRunPrcs=new FlowRunPrcs();
            flowRunPrcs.setId(id);
            flowRunPrcs.setPrcsFlag("4");
            flowRunPrcsMapper.updateFlow(flowRunPrcs);*/
            FlowAssignWithBLOBs flowAssignWithBLOBs = new FlowAssignWithBLOBs();

            int i = flowAssignMapper.updateByPrimaryKeySelective(flowAssign);
            FlowAssignWithBLOBs flowAssignWithBLOBs1 = new FlowAssignWithBLOBs();
            flowAssignWithBLOBs1.setHandleStatus(2);
            flowAssignWithBLOBs1.setParentAssignId(flowAssign.getAssignId());
            flowAssignMapper.updateByPrimaryKeySelective2(flowAssignWithBLOBs1);

            FlowAssignWithBLOBs flowAssign2 = new FlowAssignWithBLOBs();
            flowAssign2.setParentAssignId(flowAssign.getAssignId());
            List<FlowAssignWithBLOBs> flowAssignWithBLOBs3 = flowAssignMapper.selectFlowAssignByParentId(flowAssign2);
            FlowAssignWithBLOBs flowAssign1 = new FlowAssignWithBLOBs();
            flowAssign1.setPrcsId(prcsId);
            flowAssign1.setRunId(runId);
            List<FlowAssignWithBLOBs> flowAssignWithBLOBs4 = flowAssignMapper.selectFlowAssign(flowAssign1);
            for (FlowAssignWithBLOBs flowAssignWithBLOBs2 : flowAssignWithBLOBs4) {
                if (user.getUserId().equals(flowAssignWithBLOBs2.getHandleUser()) && !"2".equals(flowAssignWithBLOBs2.getHandleUser())) {
                    flowAssignWithBLOBs3.add(flowAssignWithBLOBs2);
                }
            }
            for (FlowAssignWithBLOBs flowAssignWithBLOBs2 : flowAssignWithBLOBs3) {
                if (!"2".equals(flowAssignWithBLOBs2.getHandleUser())) {
                    FlowRunPrcs flowRunPrcs = new FlowRunPrcs();
                    flowRunPrcs.setPrcsFlag("4");
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String date = formatter.format(new Date());
                    flowRunPrcs.setDeliverTime(date);
                    flowRunPrcs.setPrcsTime(date);
                    flowRunPrcs.setRunId(flowAssignWithBLOBs2.getRunId());
                    flowRunPrcs.setPrcsId(flowAssignWithBLOBs2.getPrcsId());
                    flowRunPrcs.setUserId(flowAssignWithBLOBs2.getHandleUser());
                    flowRunPrcs.setComment("1");
                    flowRunPrcsMapper.updateFlowPrcsByAssign(flowRunPrcs);
                }
            }


            String sqlType = (String) request.getSession().getAttribute("loginDateSouse");
            ContextHolder.setConsumerType("xoa" + sqlType);
            ToJson json = new ToJson();
            FlowAssignWithBLOBs flowAssignWithBLOBs2 = flowAssignMapper.selectByPrimaryKey(Integer.parseInt(assignId));
            //短信发送(判断三段是否有权限发送短信)
            //json = sms2PrivService.selectSms2("1", request);
            //短信发送权限判断模块未写完，暂默认发送
            /*smsType="5,";*/
            json.setCode("1");
            /*tableName="document";*/
            String[] strOrgAdd = smsType.split(",");//如果用户勾选，将数组遍历
            for (int j = 0; j < strOrgAdd.length; j++) {
                if (!StringUtils.checkNull(strOrgAdd[j])) {
                    switch (Integer.parseInt(strOrgAdd[j])) {
                        case 5:
                            if (!StringUtils.checkNull(flowAssignWithBLOBs2.getAssignUser())) {
                                workService.seeReminder(flowAssignWithBLOBs2.getAssignUser(), runId + "", flowId + "", prcsId + "", currentPrcsId + "", runName, tableName, tabId, user.getUserId(), sqlType, smsContent);
                            }
                        case 6:
                            if (!StringUtils.checkNull(flowAssign.getAssignUser())) {
                                workService.SMSreminder(flowAssign.getAssignUser(), runId + "", runName, json, smsContent);
                            }
                    }

                }
            }
            toJson.setFlag(0);
            toJson.setMsg("OK");
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    @Override
    public ToJson<FlowAssignWithBLOBs> selectFlowAssign(FlowAssignWithBLOBs flowAssignWithBLOBs) {
        List<FlowAssignWithBLOBs> flowAssignWithBLOBs1 = flowAssignMapper.selectFlowAssign(flowAssignWithBLOBs);
        return null;
    }

    @Override
    public ToJson<FlowAssignWithBLOBs> checkAssign(HttpServletRequest request, Integer runId,Integer prcsId,Integer flowPrcs) {
        ToJson<FlowAssignWithBLOBs> toJson = new ToJson<FlowAssignWithBLOBs>();
        try {
            List<FlowAssignWithBLOBs> flowAssigns = flowAssignMapper.checkAssign(runId,prcsId,flowPrcs);
            if (flowAssigns.size() > 0) {
                for (FlowAssignWithBLOBs flowAssign : flowAssigns) {
                    flowAssign.setHandleUserName(usersMapper.getUsernameByUserId(flowAssign.getHandleUser()));
                }
                toJson.setObj(flowAssigns);
                toJson.setFlag(0);
                toJson.setCode("0");
                toJson.setMsg("还有交办人员未办理完毕！");
            } else {
                toJson.setFlag(0);
                toJson.setCode("1");
                toJson.setMsg("已全部办理完毕");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }
}
